//
//    Record.swift
//
//    Create by 徐挺 陈 on 28/3/2018
//    Copyright © 2018. All rights reserved.
//    Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation
import SwiftyJSON
import PromiseKit


class Record : NSObject, NSCoding{
    
    var auditInfo : String!
    var auditTime : String!
    var bankId : Int!
    var certificate : String!
    var createTime : String!
    var dayInterset : Int!
    var debitId : Int!
    var debitMoney : Int!
    var debitPeroid : Int!
    var descriptionField : String!
    var overdueDay : Int!
    var overdueInterset : Float!
    var overdueMoney : Int!
    var payBackMoney : Int!
    var releaseLoanTime : String!
    var repaymentTime : String!
    var status : Int!
    var userId : Int!
    
    
    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON!){
        if json.isEmpty{
            return
        }
        auditInfo = json["auditInfo"].stringValue
        auditTime = json["auditTime"].stringValue
        bankId = json["bankId"].intValue
        certificate = json["certificate"].stringValue
        createTime = json["createTime"].stringValue
        dayInterset = json["dayInterset"].intValue
        debitId = json["debitId"].intValue
        debitMoney = json["debitMoney"].intValue
        debitPeroid = json["debitPeroid"].intValue
        descriptionField = json["description"].stringValue
        overdueDay = json["overdueDay"].intValue
        overdueInterset = json["overdueInterset"].floatValue
        overdueMoney = json["overdueMoney"].intValue
        payBackMoney = json["payBackMoney"].intValue
        releaseLoanTime = json["releaseLoanTime"].stringValue
        repaymentTime = json["repaymentTime"].stringValue
        status = json["status"].intValue
        userId = json["userId"].intValue
    }
    
    /**
     * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
     */
    func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if auditInfo != nil{
            dictionary["auditInfo"] = auditInfo
        }
        if auditTime != nil{
            dictionary["auditTime"] = auditTime
        }
        if bankId != nil{
            dictionary["bankId"] = bankId
        }
        if certificate != nil{
            dictionary["certificate"] = certificate
        }
        if createTime != nil{
            dictionary["createTime"] = createTime
        }
        if dayInterset != nil{
            dictionary["dayInterset"] = dayInterset
        }
        if debitId != nil{
            dictionary["debitId"] = debitId
        }
        if debitMoney != nil{
            dictionary["debitMoney"] = debitMoney
        }
        if debitPeroid != nil{
            dictionary["debitPeroid"] = debitPeroid
        }
        if descriptionField != nil{
            dictionary["description"] = descriptionField
        }
        if overdueDay != nil{
            dictionary["overdueDay"] = overdueDay
        }
        if overdueInterset != nil{
            dictionary["overdueInterset"] = overdueInterset
        }
        if overdueMoney != nil{
            dictionary["overdueMoney"] = overdueMoney
        }
        if payBackMoney != nil{
            dictionary["payBackMoney"] = payBackMoney
        }
        if releaseLoanTime != nil{
            dictionary["releaseLoanTime"] = releaseLoanTime
        }
        if repaymentTime != nil{
            dictionary["repaymentTime"] = repaymentTime
        }
        if status != nil{
            dictionary["status"] = status
        }
        if userId != nil{
            dictionary["userId"] = userId
        }
        return dictionary
    }
    
    /**
     * NSCoding required initializer.
     * Fills the data from the passed decoder
     */
    @objc required init(coder aDecoder: NSCoder)
    {
        auditInfo = aDecoder.decodeObject(forKey: "auditInfo") as? String
        auditTime = aDecoder.decodeObject(forKey: "auditTime") as? String
        bankId = aDecoder.decodeObject(forKey: "bankId") as? Int
        certificate = aDecoder.decodeObject(forKey: "certificate") as? String
        createTime = aDecoder.decodeObject(forKey: "createTime") as? String
        dayInterset = aDecoder.decodeObject(forKey: "dayInterset") as? Int
        debitId = aDecoder.decodeObject(forKey: "debitId") as? Int
        debitMoney = aDecoder.decodeObject(forKey: "debitMoney") as? Int
        debitPeroid = aDecoder.decodeObject(forKey: "debitPeroid") as? Int
        descriptionField = aDecoder.decodeObject(forKey: "description") as? String
        overdueDay = aDecoder.decodeObject(forKey: "overdueDay") as? Int
        overdueInterset = aDecoder.decodeObject(forKey: "overdueInterset") as? Float
        overdueMoney = aDecoder.decodeObject(forKey: "overdueMoney") as? Int
        payBackMoney = aDecoder.decodeObject(forKey: "payBackMoney") as? Int
        releaseLoanTime = aDecoder.decodeObject(forKey: "releaseLoanTime") as? String
        repaymentTime = aDecoder.decodeObject(forKey: "repaymentTime") as? String
        status = aDecoder.decodeObject(forKey: "status") as? Int
        userId = aDecoder.decodeObject(forKey: "userId") as? Int
        
    }
    
    /**
     * NSCoding required method.
     * Encodes mode properties into the decoder
     */
    func encode(with aCoder: NSCoder)
    {
        if auditInfo != nil{
            aCoder.encode(auditInfo, forKey: "auditInfo")
        }
        if auditTime != nil{
            aCoder.encode(auditTime, forKey: "auditTime")
        }
        if bankId != nil{
            aCoder.encode(bankId, forKey: "bankId")
        }
        if certificate != nil{
            aCoder.encode(certificate, forKey: "certificate")
        }
        if createTime != nil{
            aCoder.encode(createTime, forKey: "createTime")
        }
        if dayInterset != nil{
            aCoder.encode(dayInterset, forKey: "dayInterset")
        }
        if debitId != nil{
            aCoder.encode(debitId, forKey: "debitId")
        }
        if debitMoney != nil{
            aCoder.encode(debitMoney, forKey: "debitMoney")
        }
        if debitPeroid != nil{
            aCoder.encode(debitPeroid, forKey: "debitPeroid")
        }
        if descriptionField != nil{
            aCoder.encode(descriptionField, forKey: "description")
        }
        if overdueDay != nil{
            aCoder.encode(overdueDay, forKey: "overdueDay")
        }
        if overdueInterset != nil{
            aCoder.encode(overdueInterset, forKey: "overdueInterset")
        }
        if overdueMoney != nil{
            aCoder.encode(overdueMoney, forKey: "overdueMoney")
        }
        if payBackMoney != nil{
            aCoder.encode(payBackMoney, forKey: "payBackMoney")
        }
        if releaseLoanTime != nil{
            aCoder.encode(releaseLoanTime, forKey: "releaseLoanTime")
        }
        if repaymentTime != nil{
            aCoder.encode(repaymentTime, forKey: "repaymentTime")
        }
        if status != nil{
            aCoder.encode(status, forKey: "status")
        }
        if userId != nil{
            aCoder.encode(userId, forKey: "userId")
        }
        
    }



    
    class func getList() -> Promise<[Record]> {
        var arr: [Record] = Array()
        let dataFn = { (json: JSON) -> Void in
            let _ = json.array?.map({
                arr.append(Record(fromJson: $0))
            })
        }

        let request = RequestData(path: "Debit/GetUserDebitRecords")
        return netRequest(request, dataFn: dataFn).then { _ in
            return Promise(value: arr)
        }
    }
    
    class func getUserDebitAttention() -> Promise<Record> {
        var record : Record!
        let dataFn = { (json: JSON) -> Void in
            record = Record(fromJson: json)
        }
        let request = RequestData(path: "Main/GetUserDebitAttention")
        return netRequest(request, dataFn: dataFn).then { _ in
            return Promise(value: record)
        }
    }
    
    func submitPayBackDebitReuqest() -> Promise<JSON> {
        let dic: [String: Any] = [
            "certificateUrl": certificate!,
            "debitId": debitId!,
            "payBackDebitMoney": 0,
            ]
        let request = RequestData(path: "Debit/SubmitPayBackDebitRequest", param: dic)
        return netRequest(request)
    }
    
    func submitExtendReuqest() -> Promise<JSON> {
        let dic: [String: Any] = [
            "certificateUrl": certificate!,
            "debitId": debitId!,
            "payBackDebitMoney": 0,
            ]
        let request = RequestData(path: "Debit/SubmitExtendDebitRequest", param: dic)
        return netRequest(request)
    }
    
    
}
